package com.example.Blog.dao;

import com.example.Blog.model.Comment;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public interface CommentDao extends JpaRepository<Comment, Integer> {
    Comment findByCommentId(int commentId);

    @Modifying
    @Transactional
    void deleteByCommentId(int id);

    @Modifying
    @Transactional
    void deleteByCommentBelongToArticle(int articleId);

    @Modifying
    @Query("update Comment cmt set cmt.commentLikes =: commentLikes where cmt.commentId =: commentId")
    void updateByCommentId(@Param("commentLikes") int commentLikes, @Param("commentId") int commentId);

    List<Comment> findAllByCommentBelongToArticleOrderByCommentLikes(int articleId);

}